<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class YshengController extends BaseController {
	/*
	 * 养生列表
	 */
    public function ylist($tpid = null){

//		if(session('userinfo')['type']==1){
//			$where = null;
//		}else{
//			$where = array('issuer'=>session('userinfo')['username']);
//		}
		if(!empty($tpid)){
			$where['type'] = $tpid;
		}else{
			$where = null;
		}
    	$ys = D(C('TABLE_YANGSHENG'));
    	$count = $ys->where($where)->count();
    	//每页显示条数
    	$num = 12;
    	$page = new \Think\Page($count,$num);
    	$obj = pagestyle($page);
    	$show = $obj->show();
    	$result = $ys->where($where)->order('y_datetime desc')->limit(limitbakstr($obj))->select();
    	$this->assign('page',$show);
    	$this->assign('yslist',$result);
    	$this->assign('empty',article_empty('article'));
    	$this->display();
    }
    
    public function yadd(){
		$r = A('Resource');
		$this->assign('resourcelist',$r->getresourtype());
		$this->assign('resourcelist1',$r->getresourtype());
    	$this->display();
    }

 
	/*
	 * 添加
	 */
    public function add(){
    	$ys = D(C('TABLE_YANGSHENG'));
    	$rules = array(
    		array('y_title','require','标题不能为空！'),
    		//array('y_vurl','require','视频地址不能为空！'),
    	);
    	if(!$ys->validate($rules)->create()){
    		$this->error($ys->getError(),'',1);
    	}else{
    		$ys->y_img = html_entity_decode(I("post.y_img"));
    		$ys->y_audiopath = html_entity_decode(I("post.y_audiopath"));
    		$ys->y_vurl = html_entity_decode(I("post.y_vurl"));
    		$ys->content = html_entity_decode(I('post.content'));
			$ys->y_link = html_entity_decode(I('post.y_link'));
			$isc = '';
			foreach(I('post.isc') as $v){
				$isc .= $v.",";
			}
			$isc = rtrim($isc,',');
			$ys->isc = $isc;
			$ys->p_view = vieorand();
    		$result = $ys->add();
    		if($result){
    			$this->success('添加成功');
    		}else{
    			$this->error('添加失败','',1);
    		}
    	}
    }
    
    /*
     * 编辑
     */
    public function edit(){
    	$id = I('post.hiddenid');
    	if(!empty($id)){
    		$ys = D(C('TABLE_YANGSHENG'));
    		$rules = array(
    				array('y_title','require','标题不能为空！'),
    				//array('y_vurl','require','视频地址不能为空！'),
    		);
    		if(!$ys->validate($rules)->create()){
    			$this->error($ys->getError(),'',1);
    		}else{
    			$ys->y_img = html_entity_decode(I("post.y_img"));
    			$ys->y_audiopath = html_entity_decode(I("post.y_audiopath"));
    			$ys->y_vurl = html_entity_decode(I("post.y_vurl"));
    			$ys->content = html_entity_decode(I('post.content'));
				$ys->y_link = html_entity_decode(I('post.y_link'));
				$isc = '';
				foreach(I('post.isc') as $v){
					$isc .= $v.",";
				}
				$isc = rtrim($isc,',');
				$ys->isc = $isc;
				$ys->p_view = vieorand(I('post.p_view'));
    			$result = $ys->where(array('id'=>$id))->save();

    			if($result){
    				$this->success('编辑成功','ylist');
    			}else if($result==0){
					$this->success('编辑成功','ylist');
				}else{
    				$this->error('编辑失败','',1);
    			}
    		}
    	}else{
    		$this->error('参数错误','',1);
    	}

    }
    
    /*
     * 编辑页
     */
    public function yedit(){
    	$id = I('get.id');
    	if(!empty($id)){
    		$ys = D(C('TABLE_YANGSHENG'));
    		$resutl = $ys->find($id);
    		$this->assign('ys',$resutl);
    		$r = A('Resource');
    		$this->assign('resourcelist',$r->getresourtype());
    		$this->assign('resourcelist1',$r->getresourtype());
    		$this->display();
    	}else{
    		$this->error('非法参数','',1);
    	}
    }
    
    /*
     * 删除
     */
    public function ydelete(){
    	$id = I('get.id');
    	if(!empty($id)){
    		$ys = M(C('TABLE_YANGSHENG'));
    		$result = $ys->delete($id);
    		if($result){
    			$this->success('删除成功！');
    		}else{
    			$this->error('删除失败','',1);
    		}
    	}else{
    		$this->error('参数错误，删除失败','',1);
    	}

    }

	public function jjdelete(){
		$id = I('get.id');
		if(!empty($id)){
			$ys = M(C('TABLE_JIANGJING'));
			$result = $ys->delete($id);
			if($result){
				$this->success('删除成功！');
			}else{
				$this->error('删除失败','',1);
			}
		}else{
			$this->error('参数错误，删除失败','',1);
		}

	}
    
    /*****************************************************************************************/
    /*
     * 讲经列表
     */
    public function jjlist(){

		if(session('userinfo')['type']!=1){
			$where = array('issuer'=>session('userinfo')['username']);
		}else{
			$where = null;
		}
    	$ys = D(C('TABLE_JIANGJING'));
    	$count = $ys->where($where)->count();
    	//每页显示条数
    	$num = C('ARTICLE_NUM');
    	$page = new \Think\Page($count,$num);
    	$obj = pagestyle($page);
    	$show = $obj->show();
    	$result = $ys->where($where)->order(array('y_datetime'=>'DESC'))->limit(limitbakstr($obj))->select();
    	$this->assign('page',$show);
    	$this->assign('yslist',$result);
    	$this->assign('empty',article_empty('article'));
    	$this->display();
    }
    public function jjadd(){

    	$this->getjpro();
    	$this->display();
    }
    
    protected function getjpro(){
    	$d = D(C('TABLE_JPRO'));
    	$result = $d->select();
    	$this->assign('result',$result);
    }
    public function jproadd(){
    
    	$this->display();
    }
    
    public function jprolist(){
    	$ys = D(C('TABLE_JPRO'));
    	$result = $ys->select();
    	$this->assign('result',$result);
    	$this->assign('empty',article_empty('article'));
    	$this->display();
    }
    /*
     * 添加讲经人
     */
    public function jproadddata(){
    	$ys = D(C('TABLE_JPRO'));
    	$rules = array(
    		array('y_name','require','姓名不能为空！'),
    		array('y_name','','此人已存在！',0,'unique',1),
    		array('y_img','require','图片不能为空！'),
    	);
    	if(!$ys->validate($rules)->create()){
    		$this->error($ys->getError(),'',1);
    	}else{
    		$ys->y_name = I("post.y_name");
    		$ys->y_img = html_entity_decode(I("post.y_img"));
    		$ys->content = html_entity_decode(I('post.content'));
    		$result = $ys->add();
    		if($result){
    			$this->success('添加成功','jprolist');
    		}else{
    			$this->error('添加失败','',1);
    		}
    	}

    }
    /*
     * 编辑讲经人页面
     */
    public function jproedit(){
    	$id = I('get.id');
    	if($id){
    		$ys = D(C('TABLE_JPRO'));
    		$result = $ys->find($id);
    		$this->assign('result',$result);
    		$this->display();
    	}else{
    		$this->error('参数错误','',1);
    	}
    	
    }
    /*
     * 编辑讲经人
     */
    public function jproeditdata(){
    	$id = I('get.id');
    	if($id){
    		$ys = D(C('TABLE_JPRO'));
    		$rules = array(
    				array('y_name','require','姓名不能为空！'),
    				array('y_img','require','图片不能为空！'),
    		);
    		if(!$ys->validate($rules)->create()){
    			$this->error($ys->getError(),'',1);
    		}else{
    			$ys->y_name = I("post.y_name");
    			$ys->y_img = html_entity_decode(I("post.y_img"));
    			$ys->content = html_entity_decode(I('post.content'));
    			$result = $ys->where(array('id'=>$id))->save();
    			if($result){
    				$this->success('编辑成功','/Admin/Ysheng/jprolist');
    			}else{
    				$this->error('编辑失败','',1);
    			}
    		}
    	}else{
    		$this->error('参数错误','',1);
    	}    
    }
    /*
     * 删除讲经人
     */
    public function jprodelete(){
    	$id = I('get.id');
    	if($id){
    		$ys = D(C('TABLE_JPRO'));
    		$result = $ys->delete($id);
    		if($result){
    			$this->success('删除成功','/Admin/Ysheng/jprolist');
    		}else{
    			$this->error('删除失败','',1);
    		}
    	}else{
    		$this->error('参数错误','',1);
    	}
    }
    
    /*
     * 添加讲经资料
     */
    public function jjadddata(){
//    	$ys = D(C('TABLE_JIANGJING'));
//    	$rules = array(
//    			array('y_title','require','标题不能为空！'),
//    			array('y_vurl','require','音频地址不能为空！'),
//    			array('content','require','介绍不能为空！'),
//    	);
		$ys = new \Admin\Model\YshengModel;
    	if(!$ys->validate()->create()){
    		$this->error($ys->getError(),'',1);
    	}else{
    		$result = $ys->add();
    		if($result){
    			$this->success('添加成功');
    		}else{
    			$this->error('添加失败','',1);
    		}
    	}
    }
    /*
     * 编辑讲经资料
     */
    public function jjeditdata(){
    	$id = I('post.hiddenid');
    	if($id){
    		$ys = D(C('TABLE_JIANGJING'));
    		$rules = array(
    				array('y_title','require','标题不能为空！'),
    				array('y_vurl','require','音频地址不能为空！'),
    				array('content','require','介绍不能为空！'),
    		);
    		if(!$ys->validate($rules)->create()){
    			$this->error($ys->getError(),'',1);
    		}else{
    			$result = $ys->where(array('id'=>$id))->save();
    			if($result){
    				$this->success('编辑成功','/Admin/Ysheng/jjlist/');
    			}else if($result==0){
    				header('Location:/Admin/Ysheng/jjlist/');
    			}else{
    				$this->error('编辑失败','',1);
    			}
    		}
    	}else{
    		$this->error('参数错误','',1);
    	}

    }
    
    public function jjedit(){
    	
    	$id = I('get.id');
    	if($id){
    		$ys = D(C('TABLE_JIANGJING'));
    		$result = $ys->find($id);
    		$this->assign('result1',$result);
    		$this->getjpro();
    		$this->display();
    	}else{
    		$this->error('参数错误','',1);
    	}
    }

	//搜索
	public function yssearch(){
		$key = trim(I('post.key'));
		$ys = D(C('TABLE_YANGSHENG'));
		$where['y_title'] = array('LIKE',"%".$key."%");
		$count = $ys->where($where)->count();
		//每页显示条数
		$num = C('ARTICLE_NUM');
		$page = new \Think\Page($count,$num);
		$obj = pagestyle($page);
		$show = $obj->show();
		$result = $ys->where($where)->order('y_datetime desc')->limit(limitbakstr($obj))->select();
		$this->assign('page',$show);
		$this->assign('yslist',$result);
		$this->assign('empty',article_empty('article'));
		$this->display('ylist');
	}
    
}